package pers.vic.practice.module.student.service;

import org.springframework.stereotype.Service;
import pers.vic.boot.base.service.BaseService;
import pers.vic.practice.module.student.mapper.SStudentMapper;
import pers.vic.practice.module.student.model.SStudent;
import pers.vic.practice.module.student.model.SStudentInfo;

import javax.annotation.Resource;

/**
 * @description:学生表 Service
 * @author Vic.xu
 * @date: 2022-04-12 09:56
 */
@Service
public class SStudentService extends BaseService<SStudentMapper, SStudent> {

    @Resource
    private SStudentInfoService sStudentInfoService;

    @Override
    protected boolean hasAttachment() {
        return false;
    }

    @Override
    public SStudent findById(int id) {
        SStudent student = super.findById(id);
        student.setInfo(sStudentInfoService.findByNumber(student.getNumber()));
        return student;
    }

    /**
     * 根据学号查询学生
     * @param number 学号
     * @return 学生  null able
     */
    public SStudent findByNumber(String number) {
        Integer id = mapper.findId(number);
        if (id != null) {
            return findById(id);
        }
        return null;
    }

    /**
     * 先更新主表 再保存字表
     * @param student
     */
    public void saveInfo(SStudent student) {
        mapper.update(student);
        String number = student.getNumber();
        Integer infoId = sStudentInfoService.findId(number);
        SStudentInfo info = student.getInfo();
        info.setNumber(number);
        info.setId(infoId);
        sStudentInfoService.save(info);

    }
}
